package cn.iocoder.yudao.module.system.controller.admin.gugu;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.QsRankingCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.QsRankingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.QsRankingRespVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.QsRankingUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.gugu.QsRankingConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.gugu.QsRankingDO;
import cn.iocoder.yudao.module.system.service.gugu.QsRankingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 全球QS世界大学排名数据 Controller
 */
@Tag(name = "管理后台 - 全球QS世界大学排名数据")
@RestController
@RequestMapping("/system/qs-ranking")
@Validated
@Slf4j
public class QsRankingController {

    @Resource
    private QsRankingService qsRankingService;

    @PostMapping("/create")
    @Operation(summary = "创建QS排名数据")
    @PermitAll
    public CommonResult<Long> createQsRanking(@Valid @RequestBody QsRankingCreateReqVO createReqVO) {
        return success(qsRankingService.createQsRanking(BeanUtils.toBean(createReqVO, QsRankingDO.class)));
    }

    @PutMapping("/update")
    @Operation(summary = "更新QS排名数据")
    @PermitAll
    public CommonResult<Boolean> updateQsRanking(@Valid @RequestBody QsRankingUpdateReqVO updateReqVO) {
        qsRankingService.updateQsRanking(BeanUtils.toBean(updateReqVO, QsRankingDO.class));
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QS排名数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PermitAll
    public CommonResult<Boolean> deleteQsRanking(@RequestParam("id") Long id) {
        qsRankingService.deleteQsRanking(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QS排名数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<QsRankingRespVO> getQsRanking(@RequestParam("id") Long id) {
        QsRankingDO qsRanking = qsRankingService.getQsRanking(id);
        return success(BeanUtils.toBean(qsRanking, QsRankingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QS排名数据分页")
    @PermitAll
    public CommonResult<PageResult<QsRankingRespVO>> getQsRankingPage(@Valid QsRankingPageReqVO pageReqVO) {
        PageResult<QsRankingDO> pageResult = qsRankingService.getQsRankingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QsRankingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QS排名数据 Excel")
    @PermitAll
    public void exportQsRankingExcel(@Valid QsRankingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QsRankingDO> list = qsRankingService.getQsRankingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QS世界大学排名数据.xls", "数据", QsRankingRespVO.class,
                        BeanUtils.toBean(list, QsRankingRespVO.class));
    }

    @GetMapping("/fetch")
    @Operation(summary = "从API获取并保存QS排名数据")
    @Parameter(name = "appkey", description = "API密钥", required = true)
    @Parameter(name = "name", description = "学校名称，支持模糊搜索", required = false)
    @Parameter(name = "pageindex", description = "页码", required = false)
    @Parameter(name = "pagesize", description = "每页大小", required = false)
    @PermitAll
    public CommonResult<Map<String, Object>> fetchQsRanking(
            @RequestParam("appkey") String appkey,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "pageindex", required = false, defaultValue = "1") Integer pageindex,
            @RequestParam(value = "pagesize", required = false, defaultValue = "10") Integer pagesize) {
        
        Map<String, Object> result = qsRankingService.fetchAndSaveQsRanking(appkey, name, pageindex, pagesize);
        return success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "查询QS排名数据列表")
    @Parameter(name = "universityName", description = "大学名称", required = false)
    @Parameter(name = "country", description = "国家", required = false)
    @Parameter(name = "region", description = "地区", required = false)
    @PermitAll
    public CommonResult<List<QsRankingRespVO>> getQsRankingList(
            @RequestParam(value = "universityName", required = false) String universityName,
            @RequestParam(value = "country", required = false) String country,
            @RequestParam(value = "region", required = false) String region) {
        
        List<QsRankingDO> list;
        if (universityName != null && !universityName.isEmpty()) {
            list = qsRankingService.getQsRankingByUniversityName(universityName);
        } else if (country != null && !country.isEmpty()) {
            list = qsRankingService.getQsRankingByCountry(country);
        } else if (region != null && !region.isEmpty()) {
            list = qsRankingService.getQsRankingByRegion(region);
        } else {
            // 如果没有指定条件，返回前100名
            list = qsRankingService.getTopNUniversities(100);
        }
        
        return success(BeanUtils.toBean(list, QsRankingRespVO.class));
    }

    @GetMapping("/top")
    @Operation(summary = "获取排名前N的大学")
    @Parameter(name = "topN", description = "前N名", required = true)
    @PermitAll
    public CommonResult<List<QsRankingRespVO>> getTopNUniversities(@RequestParam("topN") Integer topN) {
        List<QsRankingDO> list = qsRankingService.getTopNUniversities(topN);
        return success(BeanUtils.toBean(list, QsRankingRespVO.class));
    }

    @GetMapping("/chinese")
    @Operation(summary = "获取中国大学的QS排名数据")
    @PermitAll
    public CommonResult<List<QsRankingRespVO>> getChineseUniversities() {
        List<QsRankingDO> list = qsRankingService.getChineseUniversities();
        return success(BeanUtils.toBean(list, QsRankingRespVO.class));
    }

    @GetMapping("/rank-range")
    @Operation(summary = "根据排名范围查询QS排名数据")
    @Parameter(name = "minRank", description = "最小排名", required = false)
    @Parameter(name = "maxRank", description = "最大排名", required = false)
    @PermitAll
    public CommonResult<List<QsRankingRespVO>> getQsRankingByRankRange(
            @RequestParam(value = "minRank", required = false) Integer minRank,
            @RequestParam(value = "maxRank", required = false) Integer maxRank) {
        
        List<QsRankingDO> list = qsRankingService.getQsRankingByRankRange(minRank, maxRank);
        return success(BeanUtils.toBean(list, QsRankingRespVO.class));
    }

    @DeleteMapping("/delete-by-university-id")
    @Operation(summary = "根据大学唯一标识删除QS排名数据")
    @Parameter(name = "universityId", description = "大学唯一标识", required = true)
    @PermitAll
    public CommonResult<Integer> deleteQsRankingByUniversityId(@RequestParam("universityId") String universityId) {
        int deletedCount = qsRankingService.deleteQsRankingByUniversityId(universityId);
        return success(deletedCount);
    }
}
